<?php

class TestDjForm extends UnitTestCase {

    function test1() {
        $this->sendMessage("testing DjForm API");
    }

    function testIsBound() {
        $f = new DjForm();
        $this->assertFalse($f->isBound(), "DjForm->isBound() should return false for unbound forms");

        $this->assertFalse($f->isValid(), "an unbound form's isValid() should return false");

        $f2 = new DjForm(array());
        $this->assertTrue($f2->isBound(), "DjForm->isBound() should return true for bound forms");
    }

    function testContactForm() {

        $form = new DjContactForm_test(DjContactForm_test::$valid);
        $this->assertTrue($form->isValid(), "DjForm->isValid() should return true when bound to valid data");

        $this->assertEqual($form->cleaned_data, DjContactForm_test::$clean, "then form->cleaned_data should contain cleaned data");
        // $this->dump($form);

        $form2 = new DjContactForm_test(DjContactForm_test::$invalid);
        $this->assertFalse($form2->isValid(), "DjForm->isValid() should return false when bound to invalid data");

        $this->assertEqual(count($form2->errors), 2, "then form->errors should have an entry foreach invalid key");

        // $this->dump($form2);
    }
}

class DjContactForm_test extends DjForm {

    public function __construct($data=array()) {
        $this->subject = new DjCharField();
        $this->message = new DjCharField();
        $this->sender = new DjEmailField();
        $this->cc_myself = new DjbooleanField();
        parent::__construct($data);
    }

    public static $valid = array(
        'subject' => 'hello'
        , 'message' => 'Hi there'
        , 'sender' => 'foo@example.com'
        , 'cc_myself' => true);

    public static $invalid = array(
        'subject' => ''
        , 'message' => 'Hi there'
        , 'sender' => 'invalid e-mail address'
        , 'cc_myself' => true);

    public static $clean = array(
        'subject' => 'hello'
        , 'message' => 'Hi there'
        , 'sender' => 'foo@example.com'
        , 'cc_myself' => true);
}